package org.example;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileInputStream;
import java.util.List;

/**
 * 陶渊明word检查
 */
public class WordFormatCheckerCopy {

    public static void main(String[] args) {
        try {
            XWPFDocument doc = new XWPFDocument(
                    new FileInputStream("E:\\0-学习资料\\wps备份\\word操作题 - 答案.docx")
            );
            // 开始计时
            long startTime = System.currentTimeMillis();
            List<XWPFParagraph> paragraphs = doc.getParagraphs();
            if (!paragraphs.isEmpty()) {
                // 获取第一个段落
                XWPFParagraph firstParagraph = paragraphs.get(0);
                int firstScore = checkTitleFormat(firstParagraph);
                System.out.println("第一题得分: " + firstScore + "/4");
                // 检查第二个段落
                XWPFParagraph secondParagraph = paragraphs.get(1);
                int secondScore = checkAuthor(secondParagraph);
                System.out.println("第二题得分: " + secondScore + "/4");
                // 检查第三个段落
                XWPFParagraph thirdParagraph = paragraphs.get(2);
                System.out.println("\n=== 第三题检查 ===");
                // 获取段落属性
                CTPPr pPr = thirdParagraph.getCTP().getPPr();
                // 获取行距
                CTSpacing spacing = pPr.getSpacing();
                int lastScore = 0;
                if (spacing != null) {
                    // 检查行距是否设置为25磅
                    if (spacing.getLineRule() == STLineSpacingRule.EXACT) {
                        if (spacing.getLine().toString().equals("500")) {
                            lastScore++;
                            System.out.println("✓ 行距设置为25磅（精确值）");
                        }
                    }
                    System.out.println("第三题得分: " + lastScore + "/1");
                } else {
                    System.out.println("✗ 未设置行距为25磅");
                }

                System.out.println("\n总得分: " + (firstScore + secondScore + lastScore) + "/9");
                // 停止计时
                long endTime = System.currentTimeMillis();
                System.out.println("\n耗时: " + (endTime - startTime) + "毫秒");
            } else {
                System.out.println("文档中没有段落");
            }
            doc.close();
        } catch (Exception e) {
            System.out.println("文档检查失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 检查标题格式
     *
     * @param title 标题段落
     * @return 分数
     */
    public static int checkTitleFormat(XWPFParagraph title) {
        System.out.println("\n=== 第一题检查 ===");
        int score = 0;

        if (title == null || title.getRuns().isEmpty()) {
            return 0;
        }

        // 获取段落属性
        CTPPr pPr = title.getCTP().getPPr();

        // 获取第一个运行的属性
        XWPFRun run = title.getRuns().get(0);
        CTRPr rPr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : null;

        if (rPr != null) {
            // 2. 检查字体是否为黑体
            List<CTFonts> rFontsList = rPr.getRFontsList();
            if (rFontsList != null && !rFontsList.isEmpty()) {
                CTFonts rFonts = rFontsList.get(0);
                if (rFonts != null && rFonts.getEastAsia() != null && rFonts.getEastAsia().contains("黑体")) {
                    score++;
                    System.out.println("✓ 字体设置为黑体");
                } else {
                    System.out.println("✗ 字体不是黑体");
                }
            }

            // 3. 检查字形是否为常规（非粗体非斜体）
            CTOnOff[] bArray = rPr.getBArray();
            CTOnOff[] iArray = rPr.getIArray();
            if (bArray.length == 0 && iArray.length == 0) {
                score++;
                System.out.println("✓ 字形设置为常规");
            } else {
                System.out.println("✗ 字形不是常规");
            }

            // 4. 检查字号是否为小初（36磅 = 72半磅）
            List<CTHpsMeasure> szList = rPr.getSzList();
            if (szList != null && !szList.isEmpty()) {
                CTHpsMeasure sz = szList.get(0);
                if (sz != null && sz.getVal().toString().equals("72")) {
                    score++;
                    System.out.println("✓ 字号设置为小初");
                } else {
                    System.out.println("✗ 字号不是小初");
                }
            }

            // 5. 检查空心字效果和文字居中对齐
            boolean isOutline = false;
            boolean isCenter = false;
            List<CTOnOff> outlineList = rPr.getOutlineList();
            if (outlineList != null && !outlineList.isEmpty()) {
                isOutline = true;
                System.out.println("✓ 设置了空心字效果");
            } else {
                System.out.println("✗ 未设置空心字效果");
            }
            if (pPr != null && pPr.isSetJc()) {
                CTJc jc = pPr.getJc();
                if (jc.getVal() == STJc.CENTER) {
                    isCenter = true;
                    System.out.println("✓ 居中对齐");
                } else {
                    System.out.println("✗ 未设置居中对齐");
                }
            } else {
                System.out.println("✗ 未设置居中对齐");
            }
            if (isOutline && isCenter) {
                score++;
            }
        } else {
            System.out.println("✗ 运行属性未设置");
        }

        return Math.min(score, 4); // 最多4分
    }

    public static int checkAuthor(XWPFParagraph author) {
        System.out.println("\n=== 第二题检查 ===");
        int score = 0;
        if (author == null || author.getRuns().isEmpty()) {
            return 0;
        }

        // 获取段落属性
        CTPPr pPr = author.getCTP().getPPr();

        // 获取第一个运行的属性
        XWPFRun run = author.getRuns().get(1);
        CTRPr rPr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : null;

        // 判断陶渊明字体是否设置为楷体
        if (rPr != null) {
            List<CTFonts> rFontsList = rPr.getRFontsList();
            if (rFontsList != null && !rFontsList.isEmpty()) {
                CTFonts rFonts = rFontsList.get(0);
                if (rFonts != null && rFonts.getEastAsia() != null && rFonts.getEastAsia().contains("楷体")) {
                    score++;
                    System.out.println("✓ 楷体字体");
                } else {
                    System.out.println("✗ 非楷体字体");
                }
            }

            // 检查字号是否为小三（15磅 = 30半磅）
            List<CTHpsMeasure> szList = rPr.getSzList();
            if (szList != null && !szList.isEmpty()) {
                CTHpsMeasure sz = szList.get(0);
                if (sz != null && sz.getVal().toString().equals("30")) {
                    score++;
                    System.out.println("✓ 字号设置为小三");
                } else {
                    System.out.println("✗ 字号不是小三");
                }
            }

            // 检查是否文字右对齐
            if (pPr != null && pPr.isSetJc()) {
                CTJc jc = pPr.getJc();
                if (jc.getVal() == STJc.RIGHT) {
                    score++;
                    System.out.println("✓ 右对齐");
                } else {
                    System.out.println("✗ 未设置右对齐");
                }
            }

            // 检查是否设置双边框
            List<CTBorder> bdrList = rPr.getBdrList();
            if (bdrList != null && !bdrList.isEmpty()) {
                CTBorder bdr = bdrList.get(0);
                if (bdr.getVal() == STBorder.DOUBLE) {
                    score++;
                    System.out.println("✓ 设置双边框");
                } else {
                    System.out.println("✗ 未设置双边框");
                }
            }
        } else {
            System.out.println("✗ 运行属性未设置");
        }

        return score;
    }
}